package minijvm.engine;

import minijvm.cmd.ByteCodeCommand;
import minijvm.method.Method;

/**
 * 保存{@link ByteCodeCommand}的执行结果 四种指令之一
 * @author ChangLiang
 * @date 2020/4/28
 */
public class ExecutionResult {

    /**
     * 对指令做分类
     */

    /**
     * 执行完一条再执行下面一条
     * new bipush ldc dup
     */
    public static final int RUN_NEXT_CMD = 1;

    /**
     * 跳转到另外一行指令去执行
     * if_icmpge  if_icmple  goto
     */
    public static final int JUMP = 2;

    /**
     * 退出当前栈帧
     * return
     */
    public static final int EXIT_CURRENT_FRAME = 3;

    /**
     * 暂停当前栈帧的执行 创建新的栈帧
     * invokeSpecial  invokeVirtual
     */
    public static final int PAUSE_AND_RUN_NEW_FRAME = 4;

    private int nextAction = RUN_NEXT_CMD;

    /**
     * 当处理comparision command时 需要获取到next Command Offset
     */
    private int nextCmdOffset = 0;

    /**
     * 如果是PAUSE_AND_RUN_NEW_FRAME，need to store nextMethod
     * using in invoke special and invoke virtual
     */
    private Method nextMethod;

    public Method getNextMethod() {
        return nextMethod;
    }

    public void setNextMethod(Method nextMethod) {
        this.nextMethod = nextMethod;
    }

    public int getNextCmdOffset() {
        return nextCmdOffset;
    }

    public void setNextCmdOffset(int nextCmdOffset) {
        this.nextCmdOffset = nextCmdOffset;
    }

    public void setNextAction(int nextAction) {
        this.nextAction = nextAction;
    }

    public boolean isPauseAndRunNewFrame() {
        return this.nextAction == PAUSE_AND_RUN_NEW_FRAME;
    }

    public boolean isExitCurrentFrame() {
        return this.nextAction == EXIT_CURRENT_FRAME;
    }

    public boolean isRunNextCmd() {
        return this.nextAction == RUN_NEXT_CMD;
    }

    public boolean isJump() {
        return this.nextAction == JUMP;
    }


}
